package com.heima.ThreadPool;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: Ethan Yankang
 * @Program: 面试必会代码
 * @Date: 2024-08-29 10:27
 **/
public class ThreadPool7Param {
    public static void main(String[] args) {
        /*
         * @description 创建了的线程池，核心线程数量为2，总线程数量为5(救急线程为3)，救急线程的存活时间为10millies。
         * 队列里面的任务为2的大小，使用 默认的工厂函数来创建线程，拒绝策略为使用调用者所在的线程来执行之。
         * @params
         * @return
         */
        System.err.println("当前主线程为 "+Thread.currentThread().threadId());
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,
                2,
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(2),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

        /*
         * @description 提交任务至线程池,注意是execute方法
         * @params
         * @return
         */
        for (int i = 0; i < 20; i++) {
            final int taskId = i;
            threadPoolExecutor.execute(()->{
                    System.out.println("线程"+Thread.currentThread().threadId()+" is running,TaskId="+taskId);
            });
        }

        /*
         * @description 注意线程池一定要关闭
         * @params
         * @return
         */
        threadPoolExecutor.shutdown();
    }
}
